using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class NewMainMenu_HypotheticalSubMenu : MonoBehaviour
{
	public List<TextMeshProUGUI> BackgroundTextLines = new List<TextMeshProUGUI>();

	public TextMeshProUGUI MainText;

	[SerializeField]
	private List<int> BackgroundTextMaxCount = new List<int>();

	private int CurrentSelected;

	private int previousSelected = -1;

	[SerializeField]
	private List<Hypothesis> NewHypotheticals = new List<Hypothesis>();

	[SerializeField]
	private List<Hypothesis> PlayableHypotheticals = new List<Hypothesis>();

	private bool wasEnabled;

	[SerializeField]
	private GameObject ConsoleMenu;

	public NewMainMenu_Hypothetical_ContextMenu HypotheticalContextMenu;

	public Sprite noIconSprite;

	[SerializeField]
	private List<string> storedHypotheticalNames = new List<string>();

	[SerializeField]
	private TextMeshProUGUI HypotheticalName;

	[SerializeField]
	private TextMeshProUGUI HypotheticalTagline;

	[SerializeField]
	private Image HypotheticalIcon;

	private bool AllTextShown;

	public bool AllowInput;

	private void Awake()
	{
		PlayableHypotheticals.Insert(PlayableHypotheticals.Count, null);
		CachePrefabReturnedValues();
		foreach (TextMeshProUGUI backgroundTextLine in BackgroundTextLines)
		{
			BackgroundTextMaxCount.Add(backgroundTextLine.text.Length);
			backgroundTextLine.maxVisibleCharacters = 0;
		}
		UpdateUnlockedGamblingState();
	}

	private void Update()
	{
		if (!GonerMenu.Instance.GonerMenuOpen && AllowInput)
		{
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Down))
			{
				CurrentSelected++;
				CheckCurrentSelectedOutsideBounds();
				CheckUpdateScreenText(GreenHighlighted: true);
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Up))
			{
				CurrentSelected--;
				CheckCurrentSelectedOutsideBounds();
				CheckUpdateScreenText(GreenHighlighted: true);
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Confirm) || Input.GetKeyDown(PlayerInput.Instance.Key_Menu))
			{
				SelectHypothetical();
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Cancel))
			{
				NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuDeny);
				ConsoleMenu.SetActive(value: true);
				base.gameObject.SetActive(value: false);
			}
		}
		if (!wasEnabled && base.gameObject.activeSelf)
		{
			wasEnabled = true;
			AllowInput = true;
			CurrentSelected = 0;
			previousSelected = -1;
			StartCoroutine(ShowBackgroundText());
			CheckUpdateScreenText(GreenHighlighted: true);
			UpdateUnlockedGamblingState();
		}
	}

	private void UpdateUnlockedGamblingState()
	{
		if (PlayerPrefs.GetInt("HELLOLog", 0) != 0)
		{
			return;
		}
		bool flag = true;
		foreach (Hypothesis playableHypothetical in PlayableHypotheticals)
		{
			if (playableHypothetical != null && playableHypothetical.HypotheticalReleased)
			{
				if (PlayerPrefs.GetInt("TimesPlayed_" + playableHypothetical.HypothesisName, 0) <= 0)
				{
					flag = false;
					break;
				}
				HypothesisGoal[] hypothesisGoals = playableHypothetical.HypothesisGoals;
				foreach (HypothesisGoal hypothesisGoal in hypothesisGoals)
				{
					if (PlayerPrefs.GetInt("HypothesisGoal_" + hypothesisGoal.GoalPlayprefName, 0) < hypothesisGoal.RequiredTasks)
					{
						flag = false;
						break;
					}
				}
			}
			if (!flag)
			{
				break;
			}
		}
		if (flag)
		{
			PlayerPrefs.SetInt("HELLOLog", 1);
			GonerMenu.Instance.ShowTip("日志列表里好像多了篇新日志？");
		}
	}

	private void OnDisable()
	{
		wasEnabled = false;
		previousSelected = -1;
		AllTextShown = false;
		ChangeMainText("", AllowAnimation: false);
		foreach (TextMeshProUGUI backgroundTextLine in BackgroundTextLines)
		{
			backgroundTextLine.maxVisibleCharacters = 0;
		}
	}

	private void CachePrefabReturnedValues()
	{
		storedHypotheticalNames.Clear();
		foreach (Hypothesis playableHypothetical in PlayableHypotheticals)
		{
			if (playableHypothetical != null)
			{
				storedHypotheticalNames.Add(playableHypothetical.HypothesisName);
			}
			else
			{
				storedHypotheticalNames.Add("...返回");
			}
		}
	}

	private void SelectHypothetical()
	{
		PlayerPrefs.SetString("Shortcut_ID", "");
		if (GetCurrentHypothesis() != null && !GetCurrentHypothesis().HypotheticalReleased)
		{
			if (GetCurrentHypothesis().HypotheticalReleased)
			{
				AllowInput = false;
				previousSelected = -1;
				NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuSelect);
				StartCoroutine(SelectHypotheticalTimer());
				CheckUpdateScreenText(GreenHighlighted: false);
			}
			else
			{
				NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuDeny);
			}
		}
		else
		{
			AllowInput = false;
			previousSelected = -1;
			NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuSelect);
			StartCoroutine(SelectHypotheticalTimer());
			CheckUpdateScreenText(GreenHighlighted: false);
		}
	}

	private IEnumerator SelectHypotheticalTimer()
	{
		yield return new WaitForSeconds(0.5f);
		CheckUpdateScreenText(GreenHighlighted: true);
		if (GetCurrentHypothesis() != null)
		{
			HypotheticalContextMenu.CurrentHypothesis = GetCurrentHypothesis();
			HypotheticalContextMenu.gameObject.SetActive(value: true);
		}
		else
		{
			NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuDeny);
			ConsoleMenu.SetActive(value: true);
			base.gameObject.SetActive(value: false);
		}
	}

	private void CheckCurrentSelectedOutsideBounds()
	{
		int num = NewHypotheticals.Count + PlayableHypotheticals.Count;
		if (CurrentSelected >= num)
		{
			CurrentSelected = 0;
		}
		else if (CurrentSelected < 0)
		{
			CurrentSelected = num - 1;
		}
		NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuMove);
	}

	private Hypothesis GetCurrentHypothesis()
	{
		if (CurrentSelected < NewHypotheticals.Count)
		{
			return NewHypotheticals[CurrentSelected];
		}
		return PlayableHypotheticals[CurrentSelected - NewHypotheticals.Count];
	}

	public void CheckUpdateScreenText(bool GreenHighlighted)
	{
		previousSelected = CurrentSelected;
		string text = "";
		int count = NewHypotheticals.Count;
		bool flag = false;
		if (count > 0)
		{
			text += "<color=yellow><size=42>本次更新新增猜想</size></color>\n\n";
			for (int i = 0; i < count; i++)
			{
				if (!(NewHypotheticals[i] == null))
				{
					string hypothesisName = NewHypotheticals[i].HypothesisName;
					string text2 = (IsAllQuestsCompleted(i, isNewHypothetical: true) ? "yellow" : "white");
					if (CurrentSelected == i)
					{
						flag = true;
						string text3 = "#fffc78";
						text += (GreenHighlighted ? ("<color=" + text3 + ">-> " + hypothesisName + "</color>\n") : ("<color=green>-> " + hypothesisName + "</color>\n"));
						HypotheticalName.text = hypothesisName;
						HypotheticalTagline.text = NewHypotheticals[i].HypothesisTagline;
						HypotheticalIcon.color = new Color(1f, 1f, 1f, 1f);
						HypotheticalIcon.sprite = NewHypotheticals[i].HypothesisMenuSprite ?? noIconSprite;
					}
					else
					{
						text = text + "<color=" + text2 + ">" + hypothesisName + "</color>\n";
					}
				}
			}
			text += "\n";
		}
		text += "<color=yellow><size=42>所有已发布的猜想</size></color>\n\n";
		for (int j = 0; j < storedHypotheticalNames.Count; j++)
		{
			int num = j + count;
			if (PlayableHypotheticals[j] == null)
			{
				if (CurrentSelected == num)
				{
					text += (GreenHighlighted ? "<color=yellow>-> ...返回</color>\n" : "<color=green>-> ...返回</color>\n");
					if (!flag)
					{
						HypotheticalName.text = "";
						HypotheticalTagline.text = "";
						HypotheticalIcon.sprite = noIconSprite;
						HypotheticalIcon.color = new Color(1f, 1f, 1f, 0f);
						flag = true;
					}
				}
				else
				{
					text += "...返回\n";
				}
			}
			else
			{
				if (!PlayableHypotheticals[j].HypotheticalReleased)
				{
					continue;
				}
				bool flag2 = IsAllQuestsCompleted(j);
				string text4 = (flag2 ? "yellow" : "white");
				if (PlayableHypotheticals[j].HypothesisGoals.Length == 0 && PlayerPrefs.GetInt("TimesPlayed_" + PlayableHypotheticals[j].HypothesisName, 0) > 0)
				{
					text4 = "yellow";
				}
				if (CurrentSelected == num)
				{
					string text5 = (flag2 ? "#ffc478" : "#fffc78");
					if (PlayableHypotheticals[j].HypothesisGoals.Length == 0 && PlayerPrefs.GetInt("TimesPlayed_" + PlayableHypotheticals[j].HypothesisName, 0) > 0)
					{
						text5 = "#ffc478";
					}
					text += (GreenHighlighted ? ("<color=" + text5 + ">-> " + storedHypotheticalNames[j] + "</color>\n") : ("<color=green>-> " + storedHypotheticalNames[j] + "</color>\n"));
					if (!flag)
					{
						HypotheticalName.text = storedHypotheticalNames[j];
						HypotheticalTagline.text = PlayableHypotheticals[j].HypothesisTagline;
						HypotheticalIcon.color = new Color(1f, 1f, 1f, 1f);
						HypotheticalIcon.sprite = PlayableHypotheticals[j].HypothesisMenuSprite ?? noIconSprite;
						flag = true;
					}
				}
				else
				{
					text = text + "<color=" + text4 + ">" + storedHypotheticalNames[j] + "</color>\n";
				}
			}
		}
		text += "\n<color=green>敬请期待！</color>";
		ChangeMainText(text, AllowAnimation: true);
		bool IsAllQuestsCompleted(int index, bool isNewHypothetical = false)
		{
			Hypothesis hypothesis = (isNewHypothetical ? NewHypotheticals[index] : PlayableHypotheticals[index]);
			HypothesisGoal[] hypothesisGoals = hypothesis.HypothesisGoals;
			if (PlayerPrefs.GetInt("TimesPlayed_" + hypothesis.HypothesisName, 0) <= 0)
			{
				return false;
			}
			if (hypothesisGoals == null || hypothesisGoals.Length == 0)
			{
				return true;
			}
			HypothesisGoal[] array = hypothesisGoals;
			foreach (HypothesisGoal hypothesisGoal in array)
			{
				if (PlayerPrefs.GetInt("HypothesisGoal_" + hypothesisGoal.GoalPlayprefName, 0) < hypothesisGoal.RequiredTasks)
				{
					return false;
				}
			}
			return true;
		}
	}

	private IEnumerator ShowBackgroundText()
	{
		if (!SettingsManager.Instance.GetBoolSettingValue("SimpleVFX"))
		{
			List<TextMeshProUGUI> finishedTexts = new List<TextMeshProUGUI>();
			while (!AllTextShown)
			{
				yield return null;
				foreach (TextMeshProUGUI backgroundTextLine in BackgroundTextLines)
				{
					if (!finishedTexts.Contains(backgroundTextLine) && backgroundTextLine.maxVisibleCharacters >= backgroundTextLine.text.Length)
					{
						finishedTexts.Add(backgroundTextLine);
					}
					else if (backgroundTextLine.maxVisibleCharacters < backgroundTextLine.text.Length)
					{
						float num = 100 + Random.Range(-60, -10);
						backgroundTextLine.maxVisibleCharacters += Mathf.CeilToInt(num * Time.deltaTime);
						backgroundTextLine.maxVisibleCharacters = Mathf.Clamp(backgroundTextLine.maxVisibleCharacters, 0, backgroundTextLine.text.Length);
						NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuClick);
					}
				}
				if (CompareTextLists(finishedTexts, BackgroundTextLines))
				{
					AllTextShown = true;
					MonoBehaviour.print("all text shown");
				}
			}
			yield break;
		}
		foreach (TextMeshProUGUI backgroundTextLine2 in BackgroundTextLines)
		{
			backgroundTextLine2.maxVisibleCharacters = backgroundTextLine2.text.Length;
		}
	}

	private bool CompareTextLists(List<TextMeshProUGUI> list1, List<TextMeshProUGUI> list2)
	{
		if (list1.Count != list2.Count)
		{
			return false;
		}
		for (int i = 0; i < list1.Count; i++)
		{
			if (list1[i].text != list2[i].text)
			{
				return false;
			}
		}
		return true;
	}

	public void ChangeMainText(string text, bool AllowAnimation)
	{
		MainText.text = text;
		MainText.maxVisibleCharacters = text.Length;
	}

	private IEnumerator ScrollMainText()
	{
		yield return null;
		while (MainText.maxVisibleCharacters < MainText.text.Length)
		{
			float num = 600 + Random.Range(-120, 0);
			MainText.maxVisibleCharacters += Mathf.CeilToInt(num * Time.deltaTime);
			MainText.maxVisibleCharacters = Mathf.Clamp(MainText.maxVisibleCharacters, 0, MainText.text.Length);
			NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuClick);
			yield return null;
		}
	}
}
